<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>运输管理 - 物流信息管理系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            min-height: 100vh;
            background-color: #f8f9fa;
        }
        .navbar {
            position: fixed;
            top: 0;
            right: 0;
            left: 0;
            z-index: 1030;
            height: 60px;
            background-color: #fff;
            box-shadow: 0 2px 4px rgba(0,0,0,.1);
        }
        .sidebar {
            position: fixed;
            top: 60px;
            bottom: 0;
            left: 0;
            z-index: 1020;
            width: 240px;
            background-color: #fff;
            box-shadow: 1px 0 4px rgba(0,0,0,.1);
            overflow-y: auto;
        }
        .sidebar-sticky {
            padding-top: 1rem;
        }
        .main-content {
            margin-left: 240px;
            padding: 80px 20px 20px;
            min-height: calc(100vh - 60px);
        }
        .nav-link {
            color: #333;
            padding: 10px 20px;
            display: block;
            border-radius: 4px;
            margin: 4px 8px;
        }
        .nav-link:hover {
            background-color: #f8f9fa;
            text-decoration: none;
        }
        .nav-link.active {
            background-color: #e9ecef;
            color: #0d6efd;
        }
        .nav-link i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }
        .card {
            margin-bottom: 20px;
            border: none;
            box-shadow: 0 0 10px rgba(0,0,0,.05);
            border-radius: 8px;
        }
        .table th {
            background-color: #f8f9fa;
            font-weight: 600;
        }
        .btn-icon {
            padding: 0.25rem 0.5rem;
            margin: 0 0.25rem;
        }
        .search-box {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,.05);
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-white fixed-top">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">物流信息管理系统</a>
            <div class="d-flex">
                <span class="navbar-text me-3" id="userInfo">
                    欢迎，<span id="username"></span>
                </span>
                <button class="btn btn-outline-danger btn-sm" onclick="logout()">退出登录</button>
            </div>
        </div>
    </nav>

    <!-- 侧边栏 -->
    <nav class="col-md-3 col-lg-2 d-md-block sidebar">
        <div class="sidebar-sticky">
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a class="nav-link" href="/dashboard">
                        <i class="fas fa-tachometer-alt"></i>
                        仪表盘
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/order">
                        <i class="fas fa-box"></i>
                        订单管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href="/transport">
                        <i class="fas fa-truck"></i>
                        运输管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/warehouse">
                        <i class="fas fa-warehouse"></i>
                        仓库管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/storage">
                        <i class="fas fa-database"></i>
                        存储记录
                    </a>
                </li>
            </ul>
        </div>
    </nav>

    <!-- 主要内容区域 -->
    <main class="main-content">
        <div class="container-fluid">
            <!-- 搜索和操作按钮 -->
            <div class="row mb-3">
                <div class="col-md-6">
                    <div class="input-group">
                        <input type="text" class="form-control" id="searchInput" placeholder="输入运单号搜索">
                        <button class="btn btn-primary" type="button" onclick="searchTransports()">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </div>
                </div>
                <div class="col-md-6 text-end">
                    <button class="btn btn-success" onclick="showCreateModal()">
                        <i class="fas fa-plus"></i> 新建运输
                    </button>
                </div>
            </div>

            <!-- 运输记录列表 -->
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>运单号</th>
                                    <th>订单号</th>
                                    <th>运输方式</th>
                                    <th>状态</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="transportTableBody"></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- 新建运输记录模态框 -->
    <div class="modal fade" id="createModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">新建运输记录</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="createForm">
                        <div class="mb-3">
                            <label class="form-label">订单号</label>
                            <input type="text" class="form-control" name="orderId" placeholder="请输入订单号" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">运输方式</label>
                            <select class="form-select" name="transportType" required>
                                <option value="">请选择运输方式</option>
                                <option value="公路">公路运输</option>
                                <option value="铁路">铁路运输</option>
                                <option value="航空">航空运输</option>
                                <option value="水路">水路运输</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">当前位置</label>
                            <input type="text" class="form-control" name="currentLocation" placeholder="请输入当前位置" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">状态</label>
                            <select class="form-select" name="status" required>
                                <option value="">请选择状态</option>
                                <option value="待发车">待发车</option>
                                <option value="运输中">运输中</option>
                                <option value="已到达">已到达</option>
                                <option value="已取消">已取消</option>
                            </select>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="createTransport()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 查看运输记录模态框 -->
    <div class="modal fade" id="viewModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">运输记录详情</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body" id="transportDetails"></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 编辑运输记录模态框 -->
    <div class="modal fade" id="editModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">编辑运输记录</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="editForm">
                        <input type="hidden" name="id">
                        <div class="mb-3">
                            <label class="form-label">运输方式</label>
                            <select class="form-select" name="transportType" required>
                                <option value="">请选择运输方式</option>
                                <option value="公路">公路运输</option>
                                <option value="铁路">铁路运输</option>
                                <option value="航空">航空运输</option>
                                <option value="水路">水路运输</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">当前位置</label>
                            <input type="text" class="form-control" name="currentLocation" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">状态</label>
                            <select class="form-select" name="status" required>
                                <option value="">请选择状态</option>
                                <option value="待发车">待发车</option>
                                <option value="运输中">运输中</option>
                                <option value="已到达">已到达</option>
                                <option value="已取消">已取消</option>
                            </select>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="updateTransport()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
    <script>
        $(function() {
            // 获取用户信息
            $.get('/api/user/info', function(res) {
                if (res.code === 200) {
                    $('#username').text(res.data.username);
                } else {
                    window.location.href = '/login';
                }
            });

            // 加载运输记录列表
            loadTransports();
        });

        function getStatusText(status) {
            const statusMap = {
                '待发车': '待发车',
                '运输中': '运输中',
                '已到达': '已到达',
                '已取消': '已取消'
            };
            return statusMap[status] || '未知状态';
        }

        function loadTransports() {
            $.get('/api/transport-record/list', function(res) {
                if (res.code === 200) {
                    let html = '';
                    res.data.forEach(function(transport) {
                        html += `
                            <tr>
                                <td>${transport.transportNo || ''}</td>
                                <td>${transport.orderId || ''}</td>
                                <td>${transport.transportType || ''}</td>
                                <td>${transport.status || '未知状态'}</td>
                                <td>${transport.createTime || ''}</td>
                                <td>
                                    <button class="btn btn-sm btn-info" onclick="viewTransport(${transport.id})">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                    <button class="btn btn-sm btn-primary" onclick="editTransport(${transport.id})">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                    <button class="btn btn-sm btn-danger" onclick="deleteTransport(${transport.id})">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </td>
                            </tr>
                        `;
                    });
                    $('#transportTableBody').html(html);
                }
            });
        }

        function searchTransports() {
            const transportNo = $('#searchInput').val();
            if (!transportNo) {
                loadTransports();
                return;
            }

            $.get('/api/transport-record/no/' + transportNo, function(res) {
                if (res.code === 200) {
                    let html = `
                        <tr>
                            <td>${res.data.transportNo || ''}</td>
                            <td>${res.data.orderId || ''}</td>
                            <td>${res.data.transportType || ''}</td>
                            <td>${getStatusText(res.data.status)}</td>
                            <td>${res.data.createTime || ''}</td>
                            <td>
                                <button class="btn btn-sm btn-info" onclick="viewTransport(${res.data.id})">
                                    <i class="fas fa-eye"></i>
                                </button>
                                <button class="btn btn-sm btn-primary" onclick="editTransport(${res.data.id})">
                                    <i class="fas fa-edit"></i>
                                </button>
                                <button class="btn btn-sm btn-danger" onclick="deleteTransport(${res.data.id})">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </td>
                        </tr>
                    `;
                    $('#transportTableBody').html(html);
                } else {
                    alert('未找到运输记录');
                }
            });
        }

        function showCreateModal() {
            $('#createForm')[0].reset();
            new bootstrap.Modal(document.getElementById('createModal')).show();
        }

        function createTransport() {
            const formData = {};
            $('#createForm').serializeArray().forEach(function(item) {
                formData[item.name] = item.value;
            });
            
            // 生成运单号
            formData.transportNo = 'TR' + Date.now();

            // 验证表单
            if (!formData.orderId) {
                alert('请输入订单号');
                return;
            }
            if (!formData.transportType) {
                alert('请选择运输方式');
                return;
            }
            if (!formData.currentLocation) {
                alert('请输入当前位置');
                return;
            }
            if (!formData.status) {
                alert('请选择状态');
                return;
            }

            $.ajax({
                url: '/api/transport-record',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function(res) {
                    if (res.code === 200) {
                        bootstrap.Modal.getInstance(document.getElementById('createModal')).hide();
                        loadTransports();
                    } else {
                        alert(res.message || '创建失败');
                    }
                }
            });
        }

        function viewTransport(id) {
            $.get('/api/transport-record/' + id, function(res) {
                if (res.code === 200) {
                    const transport = res.data;
                    let html = `
                        <div class="mb-3">
                            <h6>基本信息</h6>
                            <p>运单号：${transport.transportNo || ''}</p>
                            <p>订单ID：${transport.orderId || ''}</p>
                            <p>运输方式：${transport.transportType || ''}</p>
                            <p>当前位置：${transport.currentLocation || ''}</p>
                            <p>状态：${getStatusText(transport.status)}</p>
                            <p>创建时间：${transport.createTime || ''}</p>
                        </div>
                    `;
                    $('#transportDetails').html(html);
                    new bootstrap.Modal(document.getElementById('viewModal')).show();
                }
            });
        }

        function editTransport(id) {
            $.get('/api/transport-record/' + id, function(res) {
                if (res.code === 200) {
                    const transport = res.data;
                    $('#editForm input[name="id"]').val(transport.id);
                    $('#editForm select[name="transportType"]').val(transport.transportType);
                    $('#editForm input[name="currentLocation"]').val(transport.currentLocation);
                    $('#editForm select[name="status"]').val(transport.status);
                    const modal = new bootstrap.Modal(document.getElementById('editModal'));
                    modal.show();
                }
            });
        }

        function updateTransport() {
            const formData = {};
            $('#editForm').serializeArray().forEach(item => {
                if (item.value) {
                    formData[item.name] = item.value;
                }
            });

            $.ajax({
                url: '/api/transport-record',
                type: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function(res) {
                    if (res.code === 200) {
                        alert('更新成功');
                        const modal = bootstrap.Modal.getInstance(document.getElementById('editModal'));
                        modal.hide();
                        loadTransports();
                    } else {
                        alert(res.message || '更新失败');
                    }
                }
            });
        }

        function deleteTransport(id) {
            if (!confirm('确定要删除这条运输记录吗？')) return;

            $.ajax({
                url: '/api/transport-record/' + id,
                type: 'DELETE',
                success: function(res) {
                    if (res.code === 200) {
                        loadTransports();
                    } else {
                        alert(res.message || '删除失败');
                    }
                }
            });
        }

        function logout() {
            $.post('/api/user/logout', function() {
                window.location.href = '/login';
            });
        }
    </script>
</body>
</html> 